package com.startasone.controller;


import com.startasone.Utlis.JwtUtils;
import com.startasone.pojo.Emp;
import com.startasone.pojo.Result;
import com.startasone.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

import static com.startasone.Utlis.JwtUtils.generateJwt;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;
    private  JwtUtils jwtUtils = new JwtUtils();
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        log.info("查询的员工为{}",emp);
        Emp e=empService.login(emp);
        if(e!=null){
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getUsername());
            String jwt=jwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        return Result.error("账号或密码错误");
    }
}
